#!/bin/bash
source /etc/xiandian/openrc.sh
ping $HOST_IP -c 4 >> /dev/null 2>&1
if [ 0  -ne  $? ]; then
        echo -e "\033[31m Warning\nPlease make sure the network configuration is correct!\033[0m"
        exit 1
fi
# check system

sed -i  -e '/server/d' -e "/fudge/d" /etc/ntp.conf
sed -i  -e "1i server 127.127.1.0" -e "2i fudge 127.127.1.0 stratum 10" /etc/ntp.conf
systemctl restart ntpd
systemctl enable ntpd


yum install mariadb mariadb-server python2-PyMySQL expect mongodb-server mongodb rabbitmq-server memcached python-memcached -y
sed -i  "/^symbolic-links/a\default-storage-engine = innodb\ninnodb_file_per_table\ncollation-server = utf8_general_ci\ninit-connect = 'SET NAMES utf8'\ncharacter-set-server = utf8\nmax_connections=10000" /etc/my.cnf
crudini --set /usr/lib/systemd/system/mariadb.service Service LimitNOFILE 10000
crudini --set /usr/lib/systemd/system/mariadb.service Service LimitNPROC 10000
systemctl daemon-reload
systemctl enable mariadb.service
init_mariadb(){
    service_start "mariadb"
    mysql "use mysql;grant all PRIVILEGES on *.* to 'root'@'localhost' IDENTIFIED BY '$DB_PASS';"
    chek_status "$?" "数据库初始化"
    mysql_cmd "drop database test;"
    chek_status "$?" "删除测试数据库"
    mysql_cmd "use mysql;grant all PRIVILEGES on *.* to 'root'@'%' IDENTIFIED BY '$DB_PASS';"
    chek_status "$?" "数据库远程登录配置"
}
if [[ `systemctl restart mariadb.service` == '0' ]];then
        init_mariadb
else
        
# mongo
sed -i -e '/bind_ip/d' -e 's/#smallfiles.*/smallfiles=true/g' /etc/mongod.conf 
systemctl enable mongod.service
systemctl restart mongod.service
# rabbitmq
systemctl enable rabbitmq-server.service
systemctl restart rabbitmq-server.service
rabbitmqctl add_user $RABBIT_USER $RABBIT_PASS
rabbitmqctl set_permissions $RABBIT_USER ".*" ".*" ".*"
# memcache
systemctl enable memcached.service
systemctl restart memcached.service
